package com.david.func_test.viewpager

import android.os.Bundle
import android.view.View
import androidx.fragment.app.*
import com.david.func_test.R

class ViewPagerTestActivity: FragmentActivity() {
//    val fragments = ArrayList<Fragment>()
    val fragment1 = PagerFragment(1);
    val newFragment = NewFragment();
    var fragment2:Fragment = PagerFragment(2)
    val fragment3 = PagerFragment(3);
    val fragment4 = PagerFragment(4);
    val fragment5 = PagerFragment(5);

    var needUpdate = false

    lateinit var adapter:FragmentPagerAdapter
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_viewpager_test)
        val tran = supportFragmentManager.beginTransaction()
        tran.add(R.id.viewpager, fragment1)
//        tran.setCustomAnimations(R.anim.left_in, R.anim.right_out)
        tran.commit()

        findViewById<View>(R.id.change).setOnClickListener {
            if (!needUpdate) {
//                supportFragmentManager.beginTransaction().setCustomAnimations(R.anim.rotatey_in, R.anim.rotatey_out).replace(R.id.viewpager, newFragment).commit()
                supportFragmentManager.beginTransaction().setCustomAnimations(R.anim.discover_rotate_in, R.anim.discover_rotate_out).replace(R.id.viewpager, newFragment).commit()
                needUpdate = true
            } else {
                supportFragmentManager.beginTransaction().setCustomAnimations(R.anim.discover_rotate_in, R.anim.discover_rotate_out).replace(R.id.viewpager, fragment1).commit()
                needUpdate = false
            }
        }
//        (fragment2 as PagerFragment).changeFragment = object :OnChangeFragment{
//            override fun changeFragment() {
////            fragment2 = NewFragment()
////                val tag = this@ViewPagerTestActivity.fragment2.tag
////
////                val trans = supportFragmentManager.beginTransaction()
////                trans.remove(fragment2)
////                fragment2 = NewFragment()
////                trans.add(R.id.viewpager, fragment2)
////                trans.attach(fragment2)
////                trans.commit()
//
//                //如果这个fragment需要更新
//                val ft: FragmentTransaction = supportFragmentManager.beginTransaction()
//                val fragmentTag = fragment2.tag
//
//                //移除旧的fragment
//                ft.remove(fragment2)
//
//                //换成新的fragment
//                fragment2 = NewFragment()
//
////                fragment2.setfra = supportFragmentManager
//                //添加新fragment时必须用前面获得的tag ❶
//                ft.add(R.id.viewpager, fragment2, fragmentTag)
//                ft.attach(fragment2)
//                ft.commit()
//
//                needUpdate = true
////                adapter.notifyDataSetChanged()
//            }
//
//        }
//
//        val viewpager = findViewById<ViewPager>(R.id.viewpager)
//
//        adapter = object : FragmentPagerAdapter(supportFragmentManager, FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT){
//            override fun getCount(): Int {
//                return 5
//            }
//
//            override fun getItem(position: Int): Fragment {
//                return when(position) {
//                    0 ->{ return fragment1}
//                    1 ->{ return fragment2}
//                    2 ->{ return fragment3}
//                    3 ->{ return fragment4}
//                    else -> {fragment5}
//                }
//            }

//            override fun instantiateItem(container: ViewGroup, position: Int): Any {
//                return super.instantiateItem(container, position)
//            }

//            override fun instantiateItem(container: ViewGroup, position: Int): Any {
//                //得到缓存的fragment
//                var fragment = super.instantiateItem(container, position) as Fragment
//                Log.e("pager","instantiateItem...")
//
//                //得到tag
//                val fragmentTag = fragment.tag
//                if (needUpdate) {
//
//                    //如果这个fragment需要更新
//                    val ft: FragmentTransaction = supportFragmentManager.beginTransaction()
//
//                    //移除旧的fragment
//                    ft.remove(fragment)
//
//                    //换成新的fragment
//                    fragment = NewFragment()
//
//                    //添加新fragment时必须用前面获得的tag ❶
//                    ft.add(container.id, fragment, fragmentTag)
//                    ft.attach(fragment)
//                    ft.commit()
//
//
//                    //复位更新标志
//                    needUpdate = false
//                }
//                return fragment
//            }

//        }

//        viewpager.adapter = adapter
//        adapter.notifyDataSetChanged()

    }


}

interface OnChangeFragment{
    fun changeFragment()
}